/*
 * forum.js
 */
if (typeof Forum == "undefined") {
	var Forum = {
		_log : new Logger("Forum"),
		folderImgSrc : "chrome://iforum/skin/images/folder.png",
		nonFolderImgSrc : "chrome://iforum/skin/images/star.png"
	};

	Forum.load = function() {
		try {
			this._log.debug("> loading forum");

			var folders = iForumDAO.getFolders();
			for (var i = 0; i < folders.length; i++) {
				Forum.appendListItem(folders[i], false);
			}
		} catch (e) {
			this._log.error("loading forum: " + e);
		}
	};

	Forum.unload = function() {
		try {
			iForumDAO.close();
			this._log.debug("unload forum and close conn.");
		} catch (e) {
			this._log.error("unload forum: " + e);
		}
	};

	Forum.showAbout = function() {
		var dialogURL = "chrome://iforum/content/help/about.xul";
		var options = "chrome,resizable=yes,centerscreen=yes,modal,close";
		window.openDialog(dialogURL, "", options);
	};

	Forum.showLatestBookmarks = function() {
		try {
			this._log.debug("> showLatestBookmarks");
			var bookmarks = iForumDAO.getAllBookmarks(10);

			var menu = $("latest-bookmarks");
			DOMUtils.removeChildren(menu);
			if (bookmarks.length > 0) {
				for (var i = 0; i < bookmarks.length; i++) {
					var bookmark = bookmarks[i];
					var topic = iForumDAO.getTopic(bookmark.topicid);
					var folder = iForumDAO.getFolder(1);
					var url = "chrome://iforum/content/forum/thread-list.html?topicid="
							+ topic.id
							+ "&"
							+ bookmark.condition
							+ "#"
							+ bookmark.threadid;
					var title = folder.name + " - " + topic.title + " - "
							+ bookmark.createtime;
					var menuItem = document.createElement("menuitem");
					menuItem.setAttribute("class", "menuitem-iconic");
					menuItem.setAttribute("image",
							"chrome://iforum/skin/images/bookmark.png");
					menuItem.setAttribute("label", title);
					menuItem.setAttribute("oncommand",
							"Forum.go2Bookmark(this)");
					menuItem._url = url;
					menu.appendChild(menuItem);
				}
			} else {
				var menuItem = document.createElement("menuitem");
				menuItem.setAttribute("label", LocaleUtils
						.getString("no_bookmarks"));
				menu.appendChild(menuItem);
			}
		} catch (e) {
			this._log.error("showLatestBookmarks: " + e);
		}
	};

	Forum.go2Bookmark = function(elmt) {
		try {
			this._log.debug("> go2Bookmark");
			if (elmt._url) {
				$("content-body").setAttribute("src", elmt._url);
			}
		} catch (e) {
			this._log.error("go2Bookmark: " + e);
		}
	};

	Forum.showBookmarkList = function() {
		var url = "chrome://iforum/content/forum/bookmark-list.html";
		$("content-body").setAttribute("src", url);
	};

	Forum.showHelp = function() {
		var url = "chrome://iforum/content/help/help.html";
		$("content-body").setAttribute("src", url);
	};

	Forum.showOptions = function() {
		var dialogURL = "chrome://iforum/content/prefs.xul";
		var options = "chrome,resizable=yes,centerscreen=yes,modal,close";
		var params = {};
		window.openDialog(dialogURL, "", options, params);
	};

	Forum.appendListItem = function(folder, selected) {
		if (folder) {
			var listitem = document.createElement("listitem");
			listitem.setAttribute("class", "listitem-iconic");
			if (selected)
				listitem.setAttribute("selected", "true");
			listitem.setAttribute("image", Forum.folderImgSrc);
			listitem.setAttribute("label", folder.name);
			listitem.folder = folder;
			$("iforum-folders").appendChild(listitem);
		}
	};

	Forum.createFolder = function() {
		try {
			this._log.debug("> createFolder");

			var params = {
				initialName : LocaleUtils.getString("new_folder_name"),
				name : null
			};
			var dialogURL = "chrome://iforum/content/dialogs/edit-folder-dialog.xul";
			var options = "chrome,resizable=yes,centerscreen=yes,modal,close";
			window.openDialog(dialogURL, "", options, params);

			if (params.name) {
				var folder = iForumDAO.createFolder(params.name);
				Forum.appendListItem(folder, false);
			}
		} catch (e) {
			this._log.error("createFolder: " + e);
		}
	};

	Forum.editFolder = function() {
		try {
			this._log.debug("> editFolder");
			var listitem = $("iforum-folders").selectedItem;
			if (listitem && listitem.folder) {
				var folder = listitem.folder;
				var params = {
					initialName : folder.name,
					name : null
				};
				var dialogURL = "chrome://iforum/content/dialogs/edit-folder-dialog.xul";
				var options = "chrome,resizable=yes,centerscreen=yes,modal,close";
				window.openDialog(dialogURL, "", options, params);

				if (params.name) {
					if (params.name != folder.name) {
						folder.name = params.name;
						iForumDAO.updateFolder(folder);
						listitem.setAttribute("label", folder.name);
					}
				}
			}
		} catch (e) {
			this._log.error("editFolder: " + e);
		}
	};

	Forum.deleteFolder = function() {
		try {
			this._log.debug("> deleteFolder");
			var listitem = $("iforum-folders").selectedItem;
			if (listitem && listitem.folder) {
				var folder = listitem.folder;
				if (folder.id == iForumDAO.default_folder_id) {
					var msg = LocaleUtils
							.getString("cannot_delete_default_folder");
					alert(msg);
					return;
				}
				var confirmMsg = LocaleUtils.getString("confirm_delete_folder");
				confirmMsg += "\n\n" + folder.name;
				if (window.confirm(confirmMsg)) {
					var topics = iForumDAO.getTopics(folder.id);
					for (var i = 0; i < topics.length; i++) {
						var topic = topics[i];
						topic.folderid = iForumDAO.default_folder_id;
						iForumDAO.updateTopic(topic);
					}
					var bookmarks = iForumDAO.getBookmarks(folder.id);
					for (var i = 0; i < bookmarks.length; i++) {
						var bookmark = bookmarks[i];
						bookmark.folderid = iForumDAO.default_folder_id;
						iForumDAO.updateBookmark(bookmark);
					}
					iForumDAO.deleteFolder(folder.id);
					$("iforum-folders").removeChild(listitem);
				}
			}
		} catch (e) {
			this._log.error("deleteBookmarkOrFolder: " + e);
		}
	};

	Forum.onSelect = function() {
		try {
			var listitem = $("iforum-folders").selectedItem;
			if (listitem && listitem.folder) {
				var folder = listitem.folder;
				var url = "chrome://iforum/content/forum/topic-list.html?folderid="
						+ folder.id;
				$("content-body").setAttribute("src", url);
			}
		} catch (e) {
			this._log.error("onSelect: " + e);
		}
	};
}